package com.lzx.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzx.shop.dao.StoreDao;
import com.lzx.shop.dataobject.OrderDO;
import com.lzx.shop.dataobject.StoreDO;
import com.lzx.shop.params.StoreParam;
import com.lzx.shop.service.StoreService;
import com.lzx.shop.utils.UUIDUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 刘正星
 * @version 1.0.0
 * @ClassName StoreServiceImpl.java
 * @Description TODO
 * @createTime 2021年03月22日 23:57:00
 */
@Service
public class StoreServiceImpl implements StoreService {

    @Autowired
    private StoreDao storeDao;

    @Override
    public boolean save(StoreDO object) {
        //无id执行插入，有id执行更新
        if (StringUtils.isBlank(object.getId())){
            object.setId(UUIDUtil.getUUID());
            object.setGmtCreated(LocalDateTime.now());
            object.setGmtModified(LocalDateTime.now());
            return storeDao.insert(object)>0;
        }

        return storeDao.updateById(object)>0;
    }

    @Override
    public List<StoreDO> findAll() {
        return storeDao.selectList(null);
    }

    @Override
    public StoreDO findById(String id) {
        return storeDao.selectById(id);
    }

    @Override
    public boolean update(StoreDO object) {
        return storeDao.updateById(object)>0;
    }

    @Override
    public boolean DeleteById(String id) {
        return storeDao.deleteById(id)>0;
    }

    @Override
    public List<StoreDO> search(StoreParam param) {
        QueryWrapper<StoreDO> queryWrapper = new QueryWrapper();

        queryWrapper.and(wrapper->wrapper.like("store_title",param.getStoreTitle()).or()
                .like("level",param.getLevel()));
        return storeDao.selectList(queryWrapper);
    }

    @Override
    public StoreDO query(StoreParam param) {
        QueryWrapper<StoreDO> queryWrapper = new QueryWrapper();
        queryWrapper.and(wrapper->wrapper.eq("user_id",param.getUserId()));
        return storeDao.selectOne(queryWrapper);

    }
}
